{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from robomaster import robot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    指定 RoboMaster SDK 的本地ip地址（如需手动指定），在本示例中，查询得到本地的ip地址为 192.168.2.20 （比如在Windows操作系统下，通过 快捷键 Win + R 调出的窗口中输入 cmd， 然后在 CMD窗口中输入 ipconfig ， 即可以查看设备ip的信息）， 如需指定ip使用以下语句:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "robot.config.LOCAL_IP_STR = \"192.168.2.20\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    创建 Robot 类的实例对象 ep_robot， ep_robot 即一个机器人的对象:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "ep_robot = robot.Robot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    初始化机器人，如果调用初始化方法时不传入任何参数，则使用config.py中配置的默认连接方式（WIFI直连模式） 以及默认的通讯方式（udp通讯）对机器人进行初始化，在本示例中我们手动指定机器人的连接方式为组网模式, 不指定通讯方式使用默认配置:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ep_robot.initialize(conn_type=\"ap\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    可以通过以下语句设置默认的连接方式与通讯方式，本例中将默认的连接方式设置为 sta 模式， 默认的通讯方式设置为 tcp 方式:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config.DEFAULT_CONN_TYPE = \"sta\"\n",
    "config.DEFAULT_PROTO_TYPE = \"tcp\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 获取模块对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "    方法一：直接使用 . 运算符从 Robot() 对象中获取 led 对象:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from robomaster import led\n",
    "ep_robot.initialize(conn_type=\"ap\")\n",
    "ep_led = ep_robot.led"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "    方法二：利用 Robot 对象的 get_module() 方法获得指定的对象:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'led'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-55a92fcf41a1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mrobomaster\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mled\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m# ep_robot = robot.Robot()\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mep_led\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mep_robot\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_module\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"led\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mZ:\\Interet\\Anaconda\\lib\\site-packages\\robomaster\\robot.py\u001b[0m in \u001b[0;36mget_module\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m   1269\u001b[0m         \u001b[1;33m:\u001b[0m\u001b[1;32mreturn\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0m模块对象\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1270\u001b[0m         \"\"\"\n\u001b[1;32m-> 1271\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_modules\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1272\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1273\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0minitialize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconn_type\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mconfig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDEFAULT_CONN_TYPE\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mproto_type\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mconfig\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDEFAULT_PROTO_TYPE\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msn\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'led'"
     ]
    }
   ],
   "source": [
    "from robomaster import robot\n",
    "ep_robot = robot.Robot()\n",
    "ep_robot.initialize(conn_type=\"ap\")\n",
    "from robomaster import led\n",
    "# ep_robot = robot.Robot()\n",
    "ep_led = ep_robot.get_module(\"led\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 释放机器人资源"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "ep_robot.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查询接口使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    获取版本号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from robomaster import robot\n",
    "ep_robot = robot.Robot()\n",
    "ep_robot.initialize(conn_type=\"ap\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Robot Version: 01.01.1008\n"
     ]
    }
   ],
   "source": [
    "ep_version = ep_robot.get_version()\n",
    "print(\"Robot Version: {0}\".format(ep_version))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Robot Version: 01.01.1008\n"
     ]
    }
   ],
   "source": [
    "from robomaster import robot\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"ap\")\n",
    "\n",
    "    ep_version = ep_robot.get_version()\n",
    "    print(\"Robot Version: {0}\".format(ep_version))\n",
    "\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    获取SN号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-10-20 22:17:49,532 ERROR client.py:153 Client: send_sync_msg, client recv_task is not running.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Robot SN: None\n"
     ]
    }
   ],
   "source": [
    "SN = ep_robot.get_sn()\n",
    "print(\"Robot SN:\", SN)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 设置类接口的使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 整机运动模式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    机器人的运动模式有三种：自由模式（FREE）、云台跟随底盘（CHASSIS_LEAD）、底盘跟随云台（GIMBAL_LEAD）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    本示例中设置机器人的整机运动模式为底盘跟随云台模式（GIMBAL_LEAD）:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ep_robot.set_robot_mode(mode=robot.GIMBAL_LEAD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from robomaster import robot\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"ap\")\n",
    "\n",
    "    ep_robot.set_robot_mode(mode=robot.GIMBAL_LEAD)\n",
    "\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 装甲灯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from robomaster import led\n",
    "ep_led = ep_robot.led\n",
    "# ep_led = ep_robot.get_module('led')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    comp 参数选定要控制的装甲灯\n",
    "    r g b 参数指定灯的颜色\n",
    "    effect 参数指定led灯的显示效果\n",
    "    控制的装甲灯对象通过 comp 选定为所有装甲灯， r g b 颜色指定为红色， effect 选定的灯效为常亮灯效:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ep_robot.initialize(conn_type=\"ap\")\n",
    "ep_led.set_led(comp=led.COMP_ALL, r=0, g=0, b=0, effect=led.EFFECT_ON)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "brightness: 1\n",
      "brightness: 2\n",
      "brightness: 4\n",
      "brightness: 8\n",
      "brightness: 16\n",
      "brightness: 32\n",
      "brightness: 64\n",
      "brightness: 128\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "from robomaster import robot\n",
    "from robomaster import led\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"ap\")\n",
    "\n",
    "    ep_led = ep_robot.led\n",
    "\n",
    "    # 设置灯效为常亮，亮度递增\n",
    "    bright = 1\n",
    "    for i in range(0, 8):\n",
    "        ep_led.set_led(comp=led.COMP_ALL, r=bright << i, g=bright << i, b=bright << i, effect=led.EFFECT_ON)\n",
    "        time.sleep(1)\n",
    "        print(\"brightness: {0}\".format(bright << i))\n",
    "\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "4\n",
      "8\n",
      "16\n",
      "32\n",
      "64\n",
      "128\n"
     ]
    }
   ],
   "source": [
    "bright = 1\n",
    "for i in range(8):\n",
    "    # 二进制移位操作\n",
    "    print(bright << i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 动作类接口使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 射击"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from robomaster import blaster\n",
    "ep_blaster = ep_robot.blaster"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    fire_type 可以指定发射的类型， 可选水弹、红外弹，在本示例中我们使用水弹， 参数 times 设置发射的次数，本示例中指定发射的次数为1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ep_robot.initialize(conn_type=\"ap\")\n",
    "ep_blaster.fire(fire_type=blaster.WATER_FIRE, times=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from robomaster import robot\n",
    "from robomaster import blaster\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"ap\")\n",
    "\n",
    "    ep_blaster = ep_robot.blaster\n",
    "\n",
    "    # 发射1颗水弹\n",
    "    ep_blaster.fire(times=1)\n",
    "    time.sleep(2)\n",
    "\n",
    "    # 发射3颗水弹\n",
    "    ep_blaster.fire(fire_type=blaster.WATER_FIRE, times=3)\n",
    "    time.sleep(2)\n",
    "\n",
    "    # 发射1颗红外子弹\n",
    "    ep_blaster.fire(fire_type=blaster.INFRARED_FIRE)\n",
    "    time.sleep(2)\n",
    "\n",
    "    # 发射3颗红外子弹\n",
    "    ep_blaster.fire(fire_type=blaster.INFRARED_FIRE, times=3)\n",
    "    time.sleep(2)\n",
    "\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 控制底盘速度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "ep_chassis= ep_robot.chassis"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    参数 x y z 分别代表前进、横移、旋转速度\n",
    "    timeout 参数可以指定一个时间，超过该时间未接收到控制速度指令，SDK 将会主动控制机器人停止\n",
    "    timeout 为 5s, 在机器人按照指定速度运行3s后，将速度设置为 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ep_robot.initialize(conn_type=\"ap\")\n",
    "ep_chassis.drive_speed(x=0.5, y=0, z=0, timeout=5)\n",
    "time.sleep(3)\n",
    "ep_chassis.drive_speed(x=0, y=0, z=0, timeout=5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from robomaster import robot\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"ap\")\n",
    "\n",
    "    ep_chassis = ep_robot.chassis\n",
    "\n",
    "    x_val = 0.5\n",
    "    y_val = 0.3\n",
    "    z_val = 30\n",
    "\n",
    "    # 前进 3秒\n",
    "#     ep_chassis.drive_speed(x=x_val, y=0, z=0, timeout=5)\n",
    "#     time.sleep(3)\n",
    "\n",
    "    # 后退 3秒\n",
    "    ep_chassis.drive_speed(x=-x_val, y=0, z=0, timeout=5)\n",
    "    time.sleep(3)\n",
    "\n",
    "    # 左移 3秒\n",
    "    ep_chassis.drive_speed(x=0, y=-y_val, z=0, timeout=5)\n",
    "    time.sleep(3)\n",
    "\n",
    "    # 右移 3秒\n",
    "    ep_chassis.drive_speed(x=0, y=y_val, z=0, timeout=5)\n",
    "    time.sleep(3)\n",
    "\n",
    "    # 左转 3秒\n",
    "    ep_chassis.drive_speed(x=0, y=0, z=-z_val, timeout=5)\n",
    "    time.sleep(3)\n",
    "\n",
    "    # 右转 3秒\n",
    "    ep_chassis.drive_speed(x=0, y=0, z=z_val, timeout=5)\n",
    "    time.sleep(3)\n",
    "\n",
    "    # 停止麦轮运动\n",
    "    ep_chassis.drive_speed(x=0, y=0, z=0, timeout=5)\n",
    "\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 任务动作控制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 底盘\n",
    "ep_chassis = ep_robot.chassis\n",
    "# 灯\n",
    "ep_led = ep_robot.led"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    方法一：执行任务动作后，立即调用wait_for_completed() 方法:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ep_chassis.move(x=x_val, y=0, z=0, xy_speed=0.7).wait_for_completed()\n",
    "ep_led.set_led(comp=led.COMP_ALL, r=255, g=0, b=0, effect=led.EFFECT_ON)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    方法二：在执行其他命令后再调用 wait_for_completed() 方法:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "chassis_action = ep_chassis.move(x=x_val, y=0, z=0, xy_speed=0.7)\n",
    "ep_led.set_led(comp=led.COMP_ALL, r=255, g=0, b=0, effect=led.EFFECT_ON)\n",
    "chassis_action.wait_for_completed()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    方法三：不使用 wait_for_completed() 方法，利用延时来保证动作执行结束"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ep_chassis.move(x=x_val, y=0, z=0, xy_speed=0.7)\n",
    "ep_led.set_led(comp=led.COMP_ALL, r=255, g=0, b=0, effect=led.EFFECT_ON)\n",
    "time.sleep(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from robomaster import robot\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"sta\")\n",
    "\n",
    "    ep_chassis = ep_robot.chassis\n",
    "\n",
    "    x_val = 0.5\n",
    "    y_val = 0.6\n",
    "    z_val = 90\n",
    "\n",
    "    # 前进 0.5米\n",
    "    ep_chassis.move(x=x_val, y=0, z=0, xy_speed=0.7).wait_for_completed()\n",
    "\n",
    "    # 后退 0.5米\n",
    "    ep_chassis.move(x=-x_val, y=0, z=0, xy_speed=0.7).wait_for_completed()\n",
    "\n",
    "    # 左移 0.6米\n",
    "    ep_chassis.move(x=0, y=-y_val, z=0, xy_speed=0.7).wait_for_completed()\n",
    "\n",
    "    # 右移 0.6米\n",
    "    ep_chassis.move(x=0, y=y_val, z=0, xy_speed=0.7).wait_for_completed()\n",
    "\n",
    "    # 左转 90度\n",
    "    ep_chassis.move(x=0, y=0, z=z_val, z_speed=45).wait_for_completed()\n",
    "\n",
    "    # 右转 90度\n",
    "    ep_chassis.move(x=0, y=0, z=-z_val, z_speed=45).wait_for_completed()\n",
    "\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 多媒体接口"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取视频流"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from robomaster import camera\n",
    "ep_camera= ep_robot.camera"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    方法一：获取视频流并直接播放显示十秒:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ep_camera.start_video_stream(display=True, resolution=camera.STREAM_360P)\n",
    "time.sleep(10)\n",
    "ep_camera.stop_video_stream()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    方法二：获取视频流，通过cv2提供的方法显示200帧图像:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(0, 200):\n",
    "    img = ep_camera.read_cv2_image()\n",
    "    cv2.imshow(\"Robot\", img)\n",
    "    cv2.waitKey(1)\n",
    "cv2.destroyAllWindows()\n",
    "ep_camera.stop_video_stream()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    获取并直接显示视频流的例程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from robomaster import robot\n",
    "from robomaster import camera\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"ap\")\n",
    "\n",
    "    ep_camera = ep_robot.camera\n",
    "\n",
    "    # 显示十秒图传\n",
    "    ep_camera.start_video_stream(display=True, resolution=camera.STREAM_720P)\n",
    "    time.sleep(10)\n",
    "    ep_camera.stop_video_stream()\n",
    "\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "    利用cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-cb0dbcea4110>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     12\u001b[0m     \u001b[0mep_camera\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstart_video_stream\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdisplay\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mresolution\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mcamera\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSTREAM_720P\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m     \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m         \u001b[0mimg\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mep_camera\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_cv2_image\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     15\u001b[0m         \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Robot\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mimg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m         \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwaitKey\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mZ:\\Interet\\Anaconda\\lib\\site-packages\\robomaster\\camera.py\u001b[0m in \u001b[0;36mread_cv2_image\u001b[1;34m(self, timeout, strategy)\u001b[0m\n\u001b[0;32m     67\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     68\u001b[0m         \"\"\"\n\u001b[1;32m---> 69\u001b[1;33m         \u001b[0mframe\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_video_frame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstrategy\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     70\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mframe\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     71\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mZ:\\Interet\\Anaconda\\lib\\site-packages\\robomaster\\camera.py\u001b[0m in \u001b[0;36mread_video_frame\u001b[1;34m(self, timeout, strategy)\u001b[0m\n\u001b[0;32m     56\u001b[0m         \u001b[1;33m:\u001b[0m\u001b[1;32mreturn\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mframe\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m已解码的视频流帧字节流\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     57\u001b[0m         \"\"\"\n\u001b[1;32m---> 58\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_liveview\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_video_frame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstrategy\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     59\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     60\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mread_cv2_image\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstrategy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"pipeline\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mZ:\\Interet\\Anaconda\\lib\\site-packages\\robomaster\\media.py\u001b[0m in \u001b[0;36mread_video_frame\u001b[1;34m(self, timeout, strategy)\u001b[0m\n\u001b[0;32m     94\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mread_video_frame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstrategy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"pipeline\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     95\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mstrategy\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"pipeline\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 96\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_video_frame_queue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     97\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0mstrategy\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"newest\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     98\u001b[0m             \u001b[1;32mwhile\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_video_frame_queue\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mqsize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mZ:\\Interet\\Anaconda\\lib\\queue.py\u001b[0m in \u001b[0;36mget\u001b[1;34m(self, block, timeout)\u001b[0m\n\u001b[0;32m    177\u001b[0m                     \u001b[1;32mif\u001b[0m \u001b[0mremaining\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[1;36m0.0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    178\u001b[0m                         \u001b[1;32mraise\u001b[0m \u001b[0mEmpty\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 179\u001b[1;33m                     \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnot_empty\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mremaining\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    180\u001b[0m             \u001b[0mitem\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    181\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnot_full\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnotify\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mZ:\\Interet\\Anaconda\\lib\\threading.py\u001b[0m in \u001b[0;36mwait\u001b[1;34m(self, timeout)\u001b[0m\n\u001b[0;32m    298\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    299\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[0mtimeout\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 300\u001b[1;33m                     \u001b[0mgotit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwaiter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;32mTrue\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    301\u001b[0m                 \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    302\u001b[0m                     \u001b[0mgotit\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwaiter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0macquire\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "from robomaster import robot\n",
    "from robomaster import camera\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"ap\")\n",
    "\n",
    "    ep_camera = ep_robot.camera\n",
    "\n",
    "    # 显示200帧图传\n",
    "    ep_camera.start_video_stream(display=False, resolution=camera.STREAM_720P)\n",
    "    while True:\n",
    "        img = ep_camera.read_cv2_image()\n",
    "        cv2.imshow(\"Robot\", img)\n",
    "        cv2.waitKey(1)\n",
    "    cv2.destroyAllWindows()\n",
    "    ep_camera.stop_video_stream()\n",
    "\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### 获取音频流"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from robomaster import camera\n",
    "ep_camera= ep_robot.camera"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "    sample_rate 参数指定采集频率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "ep_camera.record_audio(save_file=\"output.wav\", seconds=5, sample_rate=16000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from robomaster import robot\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    ep_robot = robot.Robot()\n",
    "    ep_robot.initialize(conn_type=\"rndis\")\n",
    "\n",
    "    ep_camera = ep_robot.camera\n",
    "\n",
    "    ep_camera.record_audio(save_file=\"output.wav\", seconds=5, sample_rate=16000)\n",
    "    ep_robot.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from robomaster import version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.1.1.65\n"
     ]
    }
   ],
   "source": [
    "sdk_version = version.__version__\n",
    "print(sdk_version)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from robomaster import robot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "ep_robot = robot.Robot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ep_robot.initialize(conn_type=\"ap\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "sn = ep_robot.get_sn()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "from robomaster import led"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "ep_led = ep_robot.led"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
